package internal.generator;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.lightcommons.util.EncodingProperties;
import org.lightcommons.util.StringUtils;

/**
 * 
 * @author GL
 *
 */
public class FileHelper {

	public static String getRelativePath(File baseDir,File file) {
		String relativePath=null;
		if(baseDir.equals(file))
			relativePath = "";
		else if(baseDir.getParentFile() == null)
			relativePath = file.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
		else
			relativePath = file.getAbsolutePath().substring(baseDir.getAbsolutePath().length()+1);
		if(file.isDirectory())
			return StringUtils.cleanDirPath(relativePath);
		return StringUtils.cleanPath(relativePath);
	}
	
	public static void listFiles(File file,List<File> collector) throws IOException {
		collector.add(file);
		if((!file.isHidden() && file.isDirectory()) && !isIgnoreFile(file)) {
			File[] subFiles = file.listFiles();
			for(int i = 0; i < subFiles.length; i++) {
				if(!isIgnoreFile(subFiles[i]))
					listFiles(subFiles[i],collector);
			}
		}
	}
	
	private static boolean isIgnoreFile(File file) {
		List<String> ignoreList = new ArrayList<String>();
		ignoreList.add(".svn");
		ignoreList.add("CVS");
		for(int i = 0; i < ignoreList.size(); i++) {
			if(file.getName().equals(ignoreList.get(i))) {
				return true;
			}
		}
		if(file.getName().endsWith(".$ignore"))
			return true;
		return false;
	}
	
	public static boolean isBinary(String pathName){
		if(pathName.endsWith(".$bin"))
			return true;
		if(pathName.endsWith(".$tpl"))
			return false;
		if(pathName.contains(".$bin/") || pathName.contains(".$bin\\"))
			return true;
		String[] exts = new String[]{".js",".gif",".jpg",".jpeg",".png",".zip",".jar",".war",".rar"};
		for(String ext:exts){
			if(pathName.endsWith(ext))
				return true;
		}
		return false;
	}
	
	/**
	 * 清理文件名中的  .$bin .$tpl .$javajoin.xxx
	 * @param relativePath
	 * @return
	 */
	public static String cleanFileName(String relativePath){
		String ret = StringUtils.cleanPath(relativePath);
		ret = ret.replaceAll("\\$\\$", "%%");
		ret = ret.replaceAll("\\.\\$bin/", "/");
		
		int separatorIndex = ret.lastIndexOf("/");
		String path = (separatorIndex != -1 ? ret.substring(0,separatorIndex+1) : "");
		String fileName = (separatorIndex != -1 ? ret.substring(separatorIndex + 1) : ret);
		int sIndex = fileName.lastIndexOf(".$");
		fileName = (sIndex !=-1 ? fileName.substring(0,sIndex) : fileName);
		ret = path + fileName;
		ret = ret.replaceAll("%%", "\\$\\$");
		return ret;
	}
	
	public static boolean isTemplate(String fileName){
		return fileName.endsWith(".$tpl") || !isBinary(fileName);	
	}
	
	public static String getInsertBlock(String fileName){
		String[] parts = fileName.split("\\.");
		if(parts.length > 2 && parts[parts.length-2].startsWith("$join-")){
			String flag = parts[parts.length-1];
			String type = parts[parts.length-2].substring(1);
			String insertBlock = prop.getProperty(type);
			insertBlock = insertBlock.replace("${flag}", flag);
			return insertBlock;
		}
		return null;
	}
	
	private static Properties prop ;
	static{
		prop =EncodingProperties.loadFromClassPath("org/lightmtv/generator/join-default.conf", "utf-8");
		try{
			Properties prop2 = EncodingProperties.loadFromClassPath("org/lightmtv/generator/join.conf", "utf-8");
			prop.putAll(prop2);
		}catch(Throwable e){
		}
		
	}
}
